"""
基础环境 python3
安装包   pip install opencv-python mediapipe
作者 https://blog.csdn.net/weixin_43813200/article/details/118036079
"""
import cv2
import mediapipe as mp
import time

class poseDetector():
    def __init__(self, mode=False, model=2, smooth=True, detection=0.5, tracking=0.5):
        self.mode = mode
        self.model = model
        self.smooth = smooth
        self.detection = detection
        self.tracking = tracking

        self.mpPose = mp.solutions.pose
        
# 感觉这个 是老版本了  构造函数
        self.poses = self.mpPose.Pose(self.mode, self.model,self.smooth,self.detection, self.tracking)
        self.mpDraw = mp.solutions.drawing_utils

    def findPose(self, img, draw=True):
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        self.results = self.poses.process(imgRGB)
        print(self.results.pose_landmarks)
        if self.results.pose_landmarks:
            if draw:
                self.mpDraw.draw_landmarks(img, self.results.pose_landmarks, self.mpPose.POSE_CONNECTIONS)
        return img

    def findPosition(self, img,draw=True):
        lmList = []
        if self.results.pose_landmarks:
            myPose = self.results.pose_landmarks.landmark
            for id, lm in enumerate(myPose):
                h, w, c = img.shape
                cx, cy = int(lm.x * w), int(lm.y * h)
                lmList.append((id, cx, cy))
                if draw:
                    cv2.circle(img, (cx, cy), 10, (255, 0, 255), cv2.FILLED)
        return lmList

def main():
    pTime = 0
    video_file=0
    # '1.flv'
    cap = cv2.VideoCapture(video_file)
    detector = poseDetector()
    print("start")
    while True:
        success, img = cap.read()
        img = detector.findPose(img)
        lmList = detector.findPosition(img)
        if len(lmList) != 0:
            # print(lmList)
            pass
        cTime = time.time()
        fps = 1 / (cTime - pTime)
        pTime = cTime
        cv2.putText(img, f"fps:{int(fps)}", (10, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
        cv2.imshow('Image', img)
        cv2.waitKey(1)


    cap.release()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    main()

# G:\project\pythonProj\pose\Pose_estimation-main>G:/environment/python37/Scripts/python  -u "g:\project\pythonProj\pose\Pose_estimation-main\mediapipe系列之一肢体行为标记.py"
# Traceback (most recent call last):
#   File "g:\project\pythonProj\pose\Pose_estimation-main\mediapipe系列之一肢体行为标记.py", line 7, in <module>
#     import mediapipe as mp
#   File "G:\environment\python37\lib\site-packages\mediapipe\__init__.py", line 16, in <module>
#     from mediapipe.python import *
#   File "G:\environment\python37\lib\site-packages\mediapipe\python\__init__.py", line 17, in <module>
#     from mediapipe.python._framework_bindings import resource_util
# ModuleNotFoundError: No module named 'mediapipe.python._framework_bindings'
